﻿using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using Windows.Phone.Media.Capture;

namespace TakePhotoTogether.View
{
    public partial class NewMenu2 : UserControl
    {
        private MainPage m_mainPage;
        private const string DEBUG_TAG = "TakePhotoTogether.View.NewMenu2 => ";

        public enum State
        {
            Open,
            Closed
        }

        public State CurrentState { get; private set; }

        public NewMenu2()
        {
            InitializeComponent();
            CurrentState = State.Closed;
        }

        private void CloseStoryboard_Completed(object sender, System.EventArgs e)
        {
            LostFocus -= NewMenu2_LostFocus;
        }

        private void OpenStoryboard_Completed(object sender, System.EventArgs e)
        {
            this.Focus();
            LostFocus += NewMenu2_LostFocus;
        }

        private void NewMenu2_LostFocus(object sender, RoutedEventArgs e)
        {
            //updateState();
        }

        internal void SetParent(MainPage mainPage)
        {
            m_mainPage = mainPage;
        }

        private void MoreButton_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            UpdateState();
        }

        private void ButtonCameraMode_Click(object sender, RoutedEventArgs e)
        {
            UpdateState();
            if (m_mainPage.FrontCameraOn)
            {
                m_mainPage.frontCameraButton_Click(sender, e);
                if (m_mainPage.sensor == CameraSensorLocation.Back)
                {
                    ButtonCameraFront.Visibility = Visibility.Collapsed;
                    ButtonCameraRear.Visibility = Visibility.Visible;
                }
                else
                {
                    ButtonCameraFront.Visibility = Visibility.Visible;
                    ButtonCameraRear.Visibility = Visibility.Collapsed;
                    SetFlashState(FlashState.Off);
                }
            }
        }

        private void ButtonFlashOff_Click(object sender, RoutedEventArgs e)
        {
            UpdateState();
            if (m_mainPage.sensor == CameraSensorLocation.Back)
            {
                m_mainPage.flashButton_Click(sender, e);
                SetFlashState(FlashState.Auto);
            }
        }

        private void ButtonFlashAuto_Click(object sender, RoutedEventArgs e)
        {
            UpdateState();
            if (m_mainPage.sensor == CameraSensorLocation.Back)
            {
                m_mainPage.flashButton_Click(sender, e);
                SetFlashState(FlashState.On);
            }
        }

        private void ButtonFlashOn_Click(object sender, RoutedEventArgs e)
        {
            UpdateState();
            if (m_mainPage.sensor == CameraSensorLocation.Back)
            {
                m_mainPage.flashButton_Click(sender, e);
                SetFlashState(FlashState.Off);
            }
        }

        public void SetFlashState(FlashState state)
        {
            switch (state)
            {
                case FlashState.On:
                    ButtonFlashOn.Visibility = Visibility.Visible;
                    ButtonFlashOff.Visibility = Visibility.Collapsed;
                    ButtonFlashAuto.Visibility = Visibility.Collapsed;
                    break;
                case FlashState.Off:
                    ButtonFlashOn.Visibility = Visibility.Collapsed;
                    ButtonFlashOff.Visibility = Visibility.Visible;
                    ButtonFlashAuto.Visibility = Visibility.Collapsed;
                    break;
                case FlashState.Auto:
                    ButtonFlashOn.Visibility = Visibility.Collapsed;
                    ButtonFlashOff.Visibility = Visibility.Collapsed;
                    ButtonFlashAuto.Visibility = Visibility.Visible;
                    break;
            }
        }

        public void UpdateState()
        {
            if (CurrentState == State.Closed)
            {
                CurrentState = State.Open;
            }
            else
            {
                CurrentState = State.Closed;
                LostFocus -= NewMenu2_LostFocus;
            }

            Debug.WriteLine(DEBUG_TAG + "go to state: " + CurrentState.ToString());
            VisualStateManager.GoToState(this, CurrentState.ToString(), true);
        }

        public void CollapsedFrontCameraButton() {
            this.ButtonCameraFront.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}
